package Search;

/**
 * @author yyq
 * @create 2021-10-22 14:37
 * 二分查找
 * 前提是有序数组
 */
public class BinarySearch {

    public static int binaryFind(int[] a,int val){
        int low=0;
        int high=a.length-1;
        int mid=(low+high)/2;
        while(low<high){
            if(a[mid]==val){
                return mid;
            }
            if(a[mid]<val){
                low=mid+1;
                mid=(low+high)/2;
                continue;
            }
            if(a[mid]>val){
                high=mid-1;
                mid=(low+high)/2;
                continue;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] a={1,8, 10, 89, 1000, 1234};
        int i = binaryFind(a, 0);
        if(i==-1) System.out.println("没找到");
        else System.out.println("找到了"+i);
    }
}
